package phoenix.web.test_objects.crm;

import core.helpers.GmailHelper;
import org.openqa.selenium.WebElement;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseExt;
import phoenix.web.test_objects.data_providers.crm.MailDataProviders;

public class MailTests extends TestBaseExt
{
	public MailTests(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public MailDataProviders dataProviders = new MailDataProviders(manager);

	public void welcomeLetterTest(String p_email)
	{
		try
		{
			log().info("Welcome letter test");
			// Open letter content localy
			String htmlLocalPath = manager.mails().welcomeLetter().getPageContent(p_email);
			manager.wd().openURL(htmlLocalPath);
			String autologinText = manager.mails().welcomeLetter().getTextAutologin();
			log().info("Autologin link => " + autologinText);
			// Get Web element of Autologin
			WebElement autologin = manager.mails().welcomeLetter().getElementAutologin();
			// Set link target to "_self"
			WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
			// Click at link
			manager.wd().click(autologinLink);
			// Verify search page is loaded
			manager.web().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Welcome letter test " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
			manager.mail().close();
		}
	}

	public void matchLetterTest(String p_email, String p_userName)
	{
		try
		{
			log().info("Match letter test");
			// Open letter content localy
			String htmlLocalPath = manager.mails().matchLetter().getPageContent(p_email, p_userName);
			manager.wd().openURL(htmlLocalPath);
			String autologinText = manager.mails().matchLetter().getTextAutologin();
			log().info("Autologin link => " + autologinText);
			// Get Web element of Autologin
			WebElement autologin = manager.mails().matchLetter().getElementAutologin();
			// Set link target to "_self"
			WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
			// Click at link
			manager.wd().click(autologinLink);
			// Verify search page is loaded
			manager.web().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Match letter test " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
			manager.mail().close();
		}
	}

	public void browseLetterTest(String p_email, String p_userName)
	{
		try
		{
			log().info("Browse letter test");
			// Open letter content localy
			String htmlLocalPath = manager.mails().browseLetter().getPageContent(p_email, p_userName);
			manager.wd().openURL(htmlLocalPath);
			String autologinText = manager.mails().browseLetter().getTextAutologin();
			log().info("Autologin link => " + autologinText);
			// Get Web element of Autologin
			WebElement autologin = manager.mails().browseLetter().getElementAutologin();
			// Set link target to "_self"
			WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
			// Click at link
			manager.wd().click(autologinLink);
			// Verify search page is loaded
			manager.web().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Browse letter test " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
			manager.mail().close();
		}

	}

	public void winkLetterTest(String p_email, String p_userName)
	{
		try
		{
			log().info("Wink letter test");
			// Open letter content localy
			String htmlLocalPath = manager.mails().winkLetter().getPageContent(p_email, p_userName);
			manager.wd().openURL(htmlLocalPath);
			String autologinText = manager.mails().winkLetter().getTextAutologin();
			log().info("Autologin link => " + autologinText);
			// Get Web element of Autologin
			WebElement autologin = manager.mails().winkLetter().getElementAutologin();
			// Set link target to "_self"
			WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
			// Click at link
			manager.wd().click(autologinLink);
			// Verify search page is loaded
			manager.web().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Wink letter test " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
			manager.mail().close();
		}
	}

	public void messageLetterTest(String p_email, String p_userName)
	{
		try
		{
			log().info("Message letter test");
			// Open letter content localy
			String htmlLocalPath = manager.mails().messageLetter().getPageContent(p_email, p_userName);
			manager.wd().openURL(htmlLocalPath);
			String autologinText = manager.mails().messageLetter().getTextAutologin();
			log().info("Autologin link => " + autologinText);
			// Get Web element of Autologin
			WebElement autologin = manager.mails().messageLetter().getElementAutologin();
			// Set link target to "_self"
			WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
			// Click at link
			manager.wd().click(autologinLink);
			// Verify search page is loaded
			manager.web().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Message letter test " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
			manager.mail().close();
		}
	}

	public void forgotLetterTest(String p_email)
	{
		try
		{
			log().info("Forgot letter test");
			// Open letter content localy
			String htmlLocalPath = manager.mails().forgotLetter().getPageContent(p_email);
			manager.wd().openURL(htmlLocalPath);
			String autologinText = manager.mails().forgotLetter().getTextAutologin();
			log().info("Autologin link => " + autologinText);
			// Get Web element of Autologin
			WebElement autologin = manager.mails().forgotLetter().getElementAutologin();
			// Set link target to "_self"
			WebElement autologinLink = manager.wd().setLinkTarget(autologin, "_self");
			// Click at link
			manager.wd().click(autologinLink);
			// Verify search page is loaded
			manager.web().searchPage().waitForPageLoad();
		}
		catch (Exception p_ex)
		{
			log().errorAndScreen("Forgot letter test " + p_ex.toString());
		}
		finally
		{
			test().forErrors();
			manager.mail().close();
		}

	}
}
